import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChanceUI extends JPanel {
	private JButton btnReturn, btnDrawCard;
	private JLabel lblChanceCard;
	private Player[] pl;
	private Player p;
	private BoardUi board;
	private ChanceCards c = new ChanceCards(1);
	private JButton btnPay;
	private int chancePayment;
	private Block[] bl;

	public ChanceUI(Player currentPlayer, Chance chance, BoardUi board_, Player[] play_, Block[] bl_) {
		this.pl = play_;
		this.p = currentPlayer;
		this.board=board_;
		this.bl=bl_;
        
		setBackground(new Color(245, 222, 179));
		setLayout(null);
		
		lblChanceCard = new JLabel("");
		lblChanceCard.setBounds(0, 52, 200, 113);
		add(lblChanceCard);
		
		btnReturn = new JButton("Return");
		btnReturn.setVisible(false);
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				returnGame();
			}
		});
		btnReturn.setBounds(199, 110, 81, 23);
		add(btnReturn);
		
		JLabel lblChanceMoNa = new JLabel("Chance Mo Na");
		lblChanceMoNa.setForeground(new Color(51, 0, 0));
		lblChanceMoNa.setFont(new Font("Verdana", Font.BOLD, 18));
		lblChanceMoNa.setBounds(0, 11, 200, 30);
		add(lblChanceMoNa);
		
		btnDrawCard = new JButton("Draw a Card");
		btnDrawCard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				c.shuffleChance();
				justShowsCard();
				btnDrawCard.setVisible(false);
				if(c.getChanceRandom()!=15 && c.getChanceRandom()!=6 && c.getChanceRandom()!=2 && c.getChanceRandom()!=1){
				if(board.getCurrentPlayer().getInJail()!=true){
					btnReturn.setVisible(true);
				}else{ 
					if(board.getCurrentPlayer().getHasGetOutOfJailCard()<=0){
					JOptionPane.showMessageDialog(null,"You are in Jail na, press Susunod");
					board.getGameButtonUI().enableEndTurnBtn();
					board.getDiceUI().disableRollBtn();
					setVisible(false);
					}else if (board.getCurrentPlayer().getHasGetOutOfJailCard()>0) {
						JOptionPane.showMessageDialog(null,"You used JAIL FREE CARD ");
						board.MoveOutOfJail();
						board.getGameButtonUI().enableEndTurnBtn();

					}
				}
				}
				
		
			}
		});
		btnDrawCard.setBounds(49, 166, 106, 23);
		add(btnDrawCard);
		
		btnPay = new JButton("Pay");
		btnPay.setVisible(false);
		btnPay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Pay();
			}
		});
		btnPay.setBounds(201, 64, 79, 23);
		add(btnPay);
	}
	
	public void justShowsCard() {
		if(c.getChanceRandom()==1){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs1.jpg")));
			//ELECTED CHAIRMAN ka na! Pay Each Player 50
			for (int i = 0; i< pl.length; i++){
				if(pl[i]!=p){   //p ==the currentPlayer
					pl[i].addMoney(50);
			//		p.subtractMoney(50); //i'll move to btnPay
					this.chancePayment=i*50;
					btnPay.setVisible(true);
				}
			}
		}else if(c.getChanceRandom()==2){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs2.jpg")));
			// 'CHARGE NA LASENG' Fine 20
		//	board.getCurrentPlayer().subtractMoney(20);
			this.chancePayment=20;
			btnPay.setVisible(true);

		}else if(c.getChanceRandom()==3){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs3.jpg")));
			//LIBRENG 'Get out of Jail'
			board.getCurrentPlayer().setHasGetOutOfJailCard(board.currentPlayer.getHasGetOutOfJailCard()+1);
			
		}else if(c.getChanceRandom()==4){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs4.jpg")));
			//Building loans mo gumanda RECEIVE 150  
			board.getCurrentPlayer().addMoney(150);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			
		}else if(c.getChanceRandom()==5){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs5.jpg")));
			// Advance to Star City
			JOptionPane.showMessageDialog(null,"You Need to Move to Star City.");
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==7){
				board.moveToken(32);
			}else if(board.currentPlayer.getCurrentPosition()==22){
				board.moveToken(17);
			}else if(board.currentPlayer.getCurrentPosition()==36){
				board.moveToken(3);
			}
		}else if(c.getChanceRandom()==6){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs6.jpg")));
			 //Huleng speeding ka FINE 15
		//	board.getCurrentPlayer().subtractMoney(15);
			this.chancePayment=15;
			btnPay.setVisible(true);
			
		}else if(c.getChanceRandom()==7){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs7.jpg")));
			//Repair all your ariarian for each house PAY 50 for each hotel PAY 100
			JOptionPane.showMessageDialog(null,"Lucky, no house nor hotels.");

			
		}else if(c.getChanceRandom()==8){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs8.jpg")));
			//Banko pays you dividend of 50
			board.getCurrentPlayer().addMoney(50);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			
		}else if(c.getChanceRandom()==9){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs9.jpg")));
			//IKAW 'Go to jail'
			board.goingToJail();
			
		}else if(c.getChanceRandom()==10){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs10.jpg")));
			//Advance ka sa Dusit. If you pass GO collect 200
			JOptionPane.showMessageDialog(null,"You Need to Move to Dusit.");
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==7){
				board.moveToken(4);
			}else if(board.currentPlayer.getCurrentPosition()==22){
				board.moveToken(29); //passes Go on the way				
			}else if(board.currentPlayer.getCurrentPosition()==36){
				board.moveToken(15); //passes Go on the way
			}
	
		}else if(c.getChanceRandom()==11){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs11.jpg")));
			//Lakbay sa MRT station 2. If you pass GO collect 200
			JOptionPane.showMessageDialog(null,"You Need to Move to MRT2.");
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==7){ 
				board.moveToken(8);
			}else if(board.currentPlayer.getCurrentPosition()==22){
				board.moveToken(33); //passes Go on the way				
			}else if(board.currentPlayer.getCurrentPosition()==36){
				board.moveToken(19); //passes Go on the way
			}
		
		}else if(c.getChanceRandom()==12){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs12.jpg")));
			//IKAW 'Go back three spaces'
			JOptionPane.showMessageDialog(null,"You Need to Move 3 steps backwards");
			setVisible(false);
			this.p.setLandedOnChance(true);  //for the one moving backwards
			if(board.currentPlayer.getCurrentPosition()==7){ 
				board.moveToken(3);
			}else if(board.currentPlayer.getCurrentPosition()==22){
				board.moveToken(3); //passes Go on the way				
			}else if(board.currentPlayer.getCurrentPosition()==36){
				board.moveToken(3); //passes Go on the way
			}
			
		}else if(c.getChanceRandom()==13){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs13.jpg")));
			//Panalo sa Eat Bulaga COLLECT 100
			board.getCurrentPlayer().addMoney(100);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			
		}else if(c.getChanceRandom()==14){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs14.jpg")));
			//Advance sa Zoobic. If you pass GO collect 200
			JOptionPane.showMessageDialog(null,"You Need to Move to Zoobic.");
			setVisible(false);
			if(board.currentPlayer.getCurrentPosition()==7){ 
				board.moveToken(18);
			}else if(board.currentPlayer.getCurrentPosition()==22){
				board.moveToken(2); 		
			}else if(board.currentPlayer.getCurrentPosition()==36){
				board.moveToken(28); //passes Go on the way
			}
			
		}else if(c.getChanceRandom()==15){
			lblChanceCard.setIcon(new ImageIcon(ChanceUI.class.getResource("ChanceCards/cs15.jpg")));
			//IKAW 'Pay school fees of 150'
			this.chancePayment=150;
		//	board.getCurrentPlayer().subtractMoney(150);
			btnPay.setVisible(true);

		}
	}
	
	public void returnGame(){
		board.getDiceUI().rolledDoubles();
		setVisible(false);
	}
	
	public void Pay(){
		if(board.getCurrentPlayer().getMoney()>=chancePayment){
			p.subtractMoney(chancePayment);
			btnPay.setEnabled(false);
		//	btnPay.setVisible(true);
			btnReturn.setEnabled(true);
			btnReturn.setVisible(true);
			board.setCurrentPlayer();
			board.getPlayerPanelUI().updatePlayerMoney();
			}else{
				BankruptcyChecker bc = new BankruptcyChecker(p, bl);
				int worth=bc.checkWorth();
				
				if(worth>=chancePayment){
					JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, just mortgage your properties");
				}else{
					bc.transferAssetsBank();
					board.getCurrentPlayer().setActive(false);
					JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, Out ka na!");
					btnReturn.setVisible(true);
				}	
				}
	}
}
